package controller;

import common.action.SaveProductAction;
import common.domain.Product;
import common.form.ProductForm;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.UnsupportedEncodingException;

/**
 * 基于注解的产品控制器类
 */
@Controller
public class ProductController {

    private static final Log logger = LogFactory.getLog(ProductController.class);

    @Autowired
    private SaveProductAction saveProductAction;

    /**
     * 产品保单显示
     *
     * @return
     */
    @RequestMapping(value = "/product_input")
    public String inputProduct() {
        logger.info("inputProduct method called...");
        return "ProductForm";
    }

    /**
     * 产品保存, 请求重定向
     *
     * @param productForm        产品表单类
     * @param redirectAttributes 重定向请求
     * @return
     */
    @RequestMapping(value = "/product_save", method = RequestMethod.POST)
    public String saveProduct(ProductForm productForm, RedirectAttributes redirectAttributes) throws UnsupportedEncodingException {
        logger.info("saveProduct method called....");
        // 不需要通过request.getParameter() 接收参数在赋值了
        // 将 表单对象 转化为 领域对象
        Product product = new Product();
        product.setName(productForm.getName());
        product.setDescription(productForm.getDescription());
        try {
            product.setPrice(Float.parseFloat(productForm.getPrice()));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        // 处理业务逻辑
        Integer productId = saveProductAction.save(product);

        // 重定向视图
        redirectAttributes.addFlashAttribute("message", "The product was successfully added.");
        return "redirect:/product_view/" + productId;
    }

    /**
     * 根据 productId 查询产品信息
     */
    @RequestMapping(value = "/product_view/{id}")
    public String viewProduct(@PathVariable Integer id, Model model){
        Product product = saveProductAction.findById(id);
        model.addAttribute("product", product);
        return "ProductView";
    }

    /**
     * 注释在方法上的@ModelAttribute
     */
    @ModelAttribute
    public void testMethod(Model model){
        logger.info("ModelAttribute Annotation called .......");
        model.addAttribute("modelAttributeOnMethod", "ModelAttribute Annotation Message For Testing");
    }

    /**
     * 注释在方法参数上的ModelAttribute. 此方法有问题
     */
    //TODO 注释在方法参数上的ModelAttribute 注解
    public void modelAttributeMethodParam(@ModelAttribute("modelAttributeOnMethodParam") String str){
        logger.info("modelAttributeMethodParam Method called ..... ");
        str = "modelAttributeMethodParam String For testing";
    }

}
